<HTML>
<HEAD>
<TITLE>Charon Internet Access</TITLE>

<LINK REL="stylesheet" TYPE="text/css" HREF="charon.css"/>
<SCRIPT TYPE="text/javascript" LANG="javascript" SRC="crypto.js"></SCRIPT>
<SCRIPT TYPE="text/javascript" LANG="javascript" SRC="charon.js"></SCRIPT>
<SCRIPT TYPE="text/javascript" LANG="javascript">

function step(r, text) {
	r.lastChild.innerHTML = text;
}

var solicitation;
var proposition;
var authority;
var compliance;
var watcher;

function submitCompliance() {
	var r = document.getElementById("row3");
	change('com-submit');
	try {
	dispatchAll("/cgi-bin/charon", "document="+encodeURIComponent(compliance), function (x) {
		change('com-recv');
		if(x.status >= 300) {
			r.className = "failed";
			change('com-srverr');
			return;
		}
		if(x.responseText.substring(0, 4) == 'FAIL') {
			r.className = "failed";
			change('com-srverr');
			return;
		}
		r.className = "done";
		change('com-done');
	});
	} catch(e) {
		alert(e.message);
		r.className = "failed";
		change('com-srverr');
	}
}

function generateCompliance() {
	if(authority && watcher) {
		clearInterval(watcher);
		watcher = null;
	}
	var r = document.getElementById("row3");
	r.className = "active";
	change("com-gen");
	var c = new Compliance(proposition, authority);
	try {
		c.sign(function (x) {
			compliance = x;
			submitCompliance();
		});
	} catch(e) {
		alert(e.message);
		r.className = "failed";
		change('com-err');
	}
}

// calling xmlHttpRequest methods from here fails, presumably because it's 
// called from a sub-window context
function processAuthority(x) {
	authority = x;
	var r = document.getElementById("row2");
	r.className = "done";
	step(r, "Processing payment authority");
	//generateCompliance();
	return 1;
}

function authorityTest() {
	if(authority != null) 
		generateCompliance();
}

function bounce(x) {
	authority = null;
	if(watcher != null)
		clearInterval(watcher);
	watcher = setInterval("authorityTest()", 250);
	window.open("bounce.html?"+x, "payment");
}

function generatePaymentButton(currency, value) {
	return '<INPUT TYPE="button" VALUE="' + currency + ' ' + value + '" onClick="javascript:bounce(' + value + ')"/>';
}

function generatePaymentButtons(currency) {
	return '<DIV CLASS="center">' + 
		generatePaymentButton(currency, 1) +
		generatePaymentButton(currency, 2) +
		generatePaymentButton(currency, 5) +
		generatePaymentButton(currency, 10) +
		'</DIV>';
}

function setAll(tag, content) {
	var a = document.getElementsByTagName("SPAN");
	for(var i = 0; i < a.length; i++)
		if(a.className == tag)
			a[i].innerHTML = content;
		else
			alert("skip " + a.className);
}

function setih(id, content) {
	document.getElementById(id).innerHTML = content;
}

function processProposition() {
	var r = document.getElementById("row2");
	r.className = "active";
	//step(r, "Examining conditions");
	r.className = "paused";
	// might not always be requesting a payment, in theory...
	//step(r, "This site requires payment of " + proposition.getCurrency() + " " + proposition.getPayment() + " per " + 
		//proposition.getQuota() + " bytes of data transfer to access the Internet. How much are you willing to spend? <BR/> " +
		//generatePaymentButtons(proposition.getCurrency()));
	//setAll("currency", proposition.getCurrency());
	setih("display-currency", proposition.getCurrency());
	setih("display-quota", proposition.getQuota());
	setih("display-amount", proposition.getPayment());
	setih("display-payButtons", generatePaymentButtons(proposition.getCurrency()));
	change("prop-pay");
}

function submitSolicitation() {
	var r = document.getElementById("row1");
	change('sol-submit');
	alert(solicitation);
	dispatchAll("/cgi-bin/charon", "document="+encodeURIComponent(solicitation), function (x) {
		change('sol-recv');
		proposition = new Proposition(x);
		r.className = "done";
		change('sol-done');
		processProposition();
	});
}

function reset() {
	change("sol-start");
	change("prop-start");
	change("com-start");
}

function start() {
	var r = document.getElementById("row1");
	r.className = "active";
	change("sol-init");
	var s = new Solicitation("loki.rcpt.to");
	change("sol-sign");
	try {
		s.sign(function (x) {
			solicitation = x;
			submitSolicitation();
		});
	} catch(e) {
		r.className = "failed";
		if('noMatchingCert' == e.message) {
			change('sol-nmc');
		} else if('userCancel' == e.message) {
			change('sol-uc');
		} else if('internalError' == e.message) {
			change('sol-ie');
		} else {
			change('sol-unknown');
			throw(e);
		}
	}
}

function change(x) {
	var n = document.getElementById(x);
	if(null == n) {
		alert("No element with id '" + x + "' found");
		return;
	}
	var p = n.parentNode;
	if(p.currentMessage) {
		var o = document.getElementById(p.currentMessage);
		o.className = "optionalMessage";
	}
	p.currentMessage = x;
	n.className = "currentMessage";
}

</SCRIPT>

</HEAD>
<BODY onLoad="javascript:reset()">

<DIV CLASS="main">
<P>
	Welcome! Accessing the internet through this service takes a few simple steps. Your browser will prompt you to sign text several times. 
	These signatures are part of the process for establishing the conditions and logging in. If you have more than one key (or identity) set up in your browser,
	make sure to use the same one for all the signatures. If you don't have any, you will be prompted to create one.
</P>
<DIV CLASS="center">
<TABLE>
<TR ID="row1" CLASS="paused"><TD CLASS="index">1</TD><TD>
	<DIV CLASS="optionalMessage" ID="sol-start">
	<FORM CLASS="center">
	<INPUT TYPE="button" VALUE="Start!" onClick="javascript:start()"/>
	</FORM>
	</DIV>
	</DIV>
	<DIV CLASS="optionalMessage" ID="sol-init">
	Preparing access request
	</DIV>
	<DIV CLASS="optionalMessage" ID="sol-sign">
	Signing request - please select a key
	</DIV>
	<DIV CLASS="optionalMessage" ID="sol-done">
	Completed initial negotiation
	</DIV>
	<DIV CLASS="optionalMessage" ID="sol-recv">
	Processing response from Charon
	</DIV>
	<DIV CLASS="optionalMessage" ID="sol-submit">
	Submitting request to Charon
	</DIV>
	<DIV CLASS="optionalMessage" ID="sol-nmc">
	You don't seem to have any identities loaded in your browser. What would you like to do? <BR/>
	<SPAN CLASS="center">
	<FORM>
	<INPUT TYPE="button" VALUE="Create an identity" onClick='javascript:window.open("http://mikolaj.cx/pkcs/key.html?close=1", "keygen")'/>
	<INPUT TYPE="button" VALUE="Try again" onClick="javascript:start()"/>
	</FORM>
	</SPAN>
	</DIV>
	<DIV CLASS="optionalMessage" ID="sol-uc">
	Changed your mind? Second thoughts?
	What would you like to do? <BR/>
	<SPAN CLASS="center">
	<FORM>
	<INPUT TYPE="button" VALUE="Create an identity" onClick='javascript:window.open("http://mikolaj.cx/pkcs/key.html?close=1", "keygen")'/>
	<INPUT TYPE="button" VALUE="Try again" onClick="javascript:start()"/>
	</FORM>
	</SPAN>
	</DIV>
	<DIV CLASS="optionalMessage" ID="sol-ie">
	Uh-oh. Your browser's cryptographic engine has generated an internal error.
	What would you like to do? <BR/>
	<SPAN CLASS="center">
	<A HREF="charon.html#" onClick="javascript:start()"/>Try again</A>
	</SPAN>
	</DIV>
	<DIV CLASS="optionalMessage" ID="sol-unknown">
	Uh-oh. Your browser's cryptographic engine has generated a mysterious error.
	What would you like to do? <BR/>
	<SPAN CLASS="center">
	<A HREF="charon.html#" onClick="javascript:start()"/>Try again</A>
	</SPAN>
	</DIV>
</TD></TR>

<TR ID="row2" CLASS="pending"><TD CLASS="index">2</TD><TD>
<DIV CLASS="optionalMessage" ID="prop-start">
Waiting for Step 1: Discovering Charon's Terms and Conditions
</DIV>
<DIV CLASS="optionalMessage" ID="prop-pay">
This site requires payment of 
<SPAN CLASS="payment" ID="display-currency"></SPAN>
<SPAN CLASS="payment" ID="display-amount"></SPAN>
per
<SPAN CLASS="payment" ID="display-quota"></SPAN>
bytes of data transfer to access the Internet. How much are you willing to spend? 
<BR/>
<SPAN ID="display-payButtons"></SPAN>
</DIV>
</TD></TR>

<TR ID="row3" CLASS="pending"><TD CLASS="index">3</TD><TD>
	<DIV CLASS="optionalMessage" ID="com-start">
	Waiting for Step 2: User agreement to Terms and Conditions
	</DIV>
	<DIV CLASS="optionalMessage" ID="com-gen">
	Preparing confirmation
	</DIV>
	<DIV CLASS="optionalMessage" ID="com-err">
	Hey, doofus! You just signed something a moment ago, so what went wrong this time? <BR/>
	<FORM>
	<INPUT TYPE="button" VALUE="Try again" onClick="javascript:generateCompliance()"/>
	</FORM>
	</DIV>
	<DIV CLASS="optionalMessage" ID="com-srverr">
	Oops. The server returned an error when trying to set up your network access. You can try again, but... <BR/>
	<FORM CLASS="center">
	<INPUT TYPE="button" VALUE="Try again" onClick="javascript:submitCompliance()"/>
	</FORM>
	</DIV>
	<DIV CLASS="optionalMessage" ID="com-submit">
	Submitting confirmation to Charon
	</DIV>
	<DIV CLASS="optionalMessage" ID="com-recv">
	Processing response from Charon
	</DIV>
	<DIV CLASS="optionalMessage" ID="com-done">
	Completed transaction
	</DIV>
</TD></TR>
</TABLE>
</DIV>
</BODY>
</HTML>
